let commands = {
    'clear': () => {
        $('#terminal').html(
            `<label>>> </label>
             <input id="input-command" onkeydown="getCommand(event)" type="text" class="outline-none dark:bg-slate-900 dark:text-green-700 w-11/12">
        `
        )
    },
    'quit': () => {
        terminalToggle()
    },
    'error': (command) => {
        $('#terminal').append($('<label class="bg-white text-black dark:bg-slate-900 dark:text-green-700"></label>').text('Command not found: ' + command))
        newLine()
    }
}

function getCommand(event) {
    if (event.keyCode == 13) {
        let input = $('#input-command')
        let command = input.val()


        input.remove()
        $('#terminal').append($('<label class="bg-white text-black dark:bg-slate-900 dark:text-green-700"></label>').text(command))
        execute(command)
    }
}

function execute(command) {
    endLine()
    if (commands.hasOwnProperty(command)) {
        commands[command]()
    }
    else {
        commands['error'](command)
    }
    $('#input-command').focus()
}

function endLine() {
    $('#terminal').append($('<br>'))
}

function newLine() {
    endLine()
    $('#terminal').append($('<label class="bg-white text-black dark:bg-slate-900 dark:text-green-700"></label>').text('>> '))
    $('#terminal').append($('<input id="input-command" onkeydown="getCommand(event)" type="text" class="outline-none dark:bg-slate-900 dark:text-green-700 w-11/12">'))
}

function setFocus() {
    $('#input-command').focus()
}

function terminalToggle() {
    if ($('#terminal').is(":hidden")) {
        $('#terminal').show()
        $('#map').height('68%')
    }
    else {
        $('#terminal').hide()
        $('#map').height('89%')
    }
}

function popupRequest() {
    $('#request-box').show()
}